package com.yanqu.road.dao.impl.cross.banquet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.auto.CrossBanquetAutoData;
import com.yanqu.road.entity.banquet.auto.CrossBanquetAutoJoinLog;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBanquetAutoDaoImpl extends TempDataObjectDao<CrossBanquetAutoData> {

    public Map<Long, CrossBanquetAutoData> getCrossBanquetAutoMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        //假如在开宴会的时候出现异常了，重启了就不管了 这里改了CrossBanquetMemberDaoImpl这个也要改一下啊
        String sql = "select * from t_c_banquet_auto where activityId = ?";
        Map<Long, CrossBanquetAutoData> res = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, activityId);

            rs = call.executeQuery();
            while (rs.next()) {
                CrossBanquetAutoData data = parse(rs);
                res.put(data.getUserId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return res;
    }

    @Override
    protected boolean addData(CrossBanquetAutoData data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_c_banquet_auto(activityId,userId,titleAdd,skillAdd,serverId,itemMap,allIntegral,allPopularity,joinTimes,ingots,joinTimesMap)" +
                " VALUES(?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getTitleAdd());
            call.setInt(i++, data.getSkillAdd());
            call.setLong(i++, data.getServerId());
            call.setString(i++, JSON.toJSONString(data.getItemMap()));
            call.setLong(i++, data.getAllIntegral());
            call.setLong(i++, data.getAllPopularity());
            call.setInt(i++, data.getJoinTimes());
            call.setLong(i++, data.getIngots());
            call.setString(i++, JSON.toJSONString(data.getJoinTimesMap()));

            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(CrossBanquetAutoData data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_c_banquet_auto SET titleAdd=?,skillAdd=?,serverId=?,itemMap=?,allIntegral=?,allPopularity=?,joinTimes=?,ingots=?,joinTimesMap=? " +
                "where activityId=? AND userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getTitleAdd());
            call.setInt(i++, data.getSkillAdd());
            call.setLong(i++, data.getServerId());
            call.setString(i++, JSON.toJSONString(data.getItemMap()));
            call.setLong(i++, data.getAllIntegral());
            call.setLong(i++, data.getAllPopularity());
            call.setInt(i++, data.getJoinTimes());
            call.setLong(i++, data.getIngots());
            call.setString(i++, JSON.toJSONString(data.getJoinTimesMap()));


            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected CrossBanquetAutoData parseData(ResultSet rs) throws SQLException {
        CrossBanquetAutoData data = new CrossBanquetAutoData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setTitleAdd(rs.getInt("titleAdd"));
        data.setSkillAdd(rs.getInt("skillAdd"));
        data.setServerId(rs.getLong("serverId"));
        data.setAllIntegral(rs.getLong("allIntegral"));
        data.setAllPopularity(rs.getLong("allPopularity"));
        data.setJoinTimes(rs.getInt("joinTimes"));
        data.setIngots(rs.getInt("ingots"));
        String itemMapStr = rs.getString("itemMap");
        if(null == itemMapStr){
            itemMapStr = "{}";
        }
        Map<String, Integer> itemMap = JSON.parseObject(itemMapStr, new TypeReference<ConcurrentHashMap<String, Integer>>() {});
        if(null != itemMap){
            data.setItemMap(itemMap);
        }

        String joinTimesMapStr = rs.getString("joinTimesMap");
        if(null == joinTimesMapStr){
            joinTimesMapStr = "{}";
        }
        Map<Integer, Integer> joinTimesMap = JSON.parseObject(joinTimesMapStr, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {});
        if(null != joinTimesMap){
            data.setJoinTimesMap(joinTimesMap);
        }
        return data;
    }
}